package com.yanqu.road.server.logic.task.condition;

import com.yanqu.road.entity.config.passroad.PassRoadPointInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserPassRoad;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.server.manger.config.PassRoadMgr;

import java.math.BigInteger;

/**
 * 通关指定关卡
 */
public class PassBattleCondition extends BaseCondition {

	public PassBattleCondition(TaskConditionInfo conditionInfo, BaseTaskData quest, BigInteger value, Object initArgs) {
		super(conditionInfo, quest, value, initArgs);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.PassBattle.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.PassBattle.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		BigInteger param4 = taskConditionInfo.getParamList().get(3);
		return getValue().compareTo(param4) >= 0;
	}

	@Override
	protected void changeValue(Object object) {
		UserPassRoad args = (UserPassRoad)object;
		if(null != args){
			PassRoadPointInfo info = PassRoadMgr.getPassRoadPointInfo(args.getPassId(),args.getPassProgress());
			if(info == null){
				info = PassRoadMgr.getLastPassRoadPointInfo();
			}
			//每一个关卡有6小节，如果不是6小节就炸了
			int passId = (info.getId() - 1) * 6 + info.getSessionId();
			setValue(BigInteger.valueOf(passId));
		}
	}
}
